var dictionaryDetailManager = {
    searchUrl: '/hzs/dictionarydetail/pageFindByHsdtId',
    saveUrl: "/hzs/dictionarydetail/save",
    editUrl: "/hzs/dictionarydetail/edit",
    removeUrl: "/hzs/dictionarydetail/remove",
    typeData: null,
    whereData: null,
    detailInfoLayer: null,
    isAdd: false,
    detailTable: null,
    detailTree: null,
    init: function () {
        layui.use(['table', 'util', 'layer', 'form', 'tree'], function () {
            dictionaryDetailManager.refreshTable();
            layui.form.on('submit(detailSearch)', dictionaryDetailManager.search);
        });
    },

    // 刷新表格
    refreshTable: function () {
        dictionaryDetailManager.detailTable = hs_utils.renderPageTable(dictionaryDetailManager.tableConfig(), dictionaryDetailManager.eventConfig());
    },

    // 打开角色信息窗口
    openFormLayer: function () {
        dictionaryDetailManager.detailInfoLayer = layui.layer.open({
            type: 1,
            title: '字典明细',
            area: ["450px", "360px"],
            content: $("#dictionaryDetailDiv")
        });
        layui.form.on('submit(dictionaryDetailSave)', dictionaryDetailManager.submit)
    },

    // 提交数据
    submit: function (data) {
        let url_ = dictionaryDetailManager.editUrl;
        if (dictionaryDetailManager.isAdd) {
            url_ = dictionaryDetailManager.saveUrl;
        }
        hs_utils.post(url_, data.field, function (data) {
            if (data.success) {
                layui.layer.close(dictionaryDetailManager.detailInfoLayer);
                layui.layer.msg("操作成功！");
                dictionaryDetailManager.reloadTable();
            } else {
                layui.layer.msg(data.msg);
            }
        });
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    }
    ,

    // 删除
    remove: function (hsddId) {
        hs_utils.post(dictionaryDetailManager.removeUrl, {hsddId: hsddId}, function (data) {
            if (data.success) {
                layui.layer.msg("操作成功！");
                dictionaryDetailManager.reloadTable();
            } else {
                layui.layer.msg(data.msg);
            }
        });
    },

    // 查询
    search: function (data) {
        dictionaryDetailManager.reloadTable(data.field);
        return false;
    },

    // 切换字典类型
    setTypeData: function (typeData) {
        // 清空查询条件
        $('#searchHsddName').val('');
        // 刷新明细数据
        dictionaryDetailManager.typeData = typeData;
        dictionaryDetailManager.reloadTable({});
    },

    // 重载表格
    reloadTable: function (data) {
        if (data && dictionaryDetailManager.typeData) {
            data.hsdtId = dictionaryDetailManager.typeData.hsdtId;
        }
        if (data) {
            dictionaryDetailManager.whereData = hs_utils.mergeWhereData(dictionaryDetailManager.whereData, data);
            dictionaryDetailManager.detailTable.reload({
                where: dictionaryDetailManager.whereData
            });
        } else {
            dictionaryDetailManager.detailTable.reload();
        }
    },

    // 表格参数
    tableConfig: function () {
        return {
            elem: '#detailTable',
            url: dictionaryDetailManager.searchUrl, //数据接口
            toolbar: '#detailTableToolBar',
            cols: [[ //表头
                {field: 'hsddName', title: '明细名称', align: "center"},
                {
                    field: 'intValue',
                    title: '明细数值',
                    align: "center"
                },
                {
                    field: 'strValue',
                    title: '明细字符值',
                    align: "center"
                },
                {field: 'sortNum', title: '排序号', align: "center"},
                {
                    field: 'hsddId', title: '操作', align: "center",
                    toolbar: "#detailTableItemToolBar"
                }
            ]]
        };
    },

    // 表格事件参数
    eventConfig: function () {
        return {
            filter: 'detailTable',
            toolbar: [
                {
                    name: 'add', handler: function (obj) {
                        if (!dictionaryDetailManager.typeData) {
                            layui.layer.msg("请先选择字典类型！");
                        } else {
                            $('#dictionaryDetailForm')[0].reset();
                            layui.form.val('dictionaryDetailForm', {
                                hsddId: '',
                                hsdtId: dictionaryDetailManager.typeData.hsdtId,
                                parentId: ''
                            });
                            dictionaryDetailManager.isAdd = true;
                            dictionaryDetailManager.openFormLayer();
                        }
                    }
                }
            ],
            tool: [
                {
                    name: 'remove', handler: function (obj) {
                        layui.layer.confirm('真的删除行么', function (index) {
                            layui.layer.close(index);
                            //向服务端发送删除指令
                            dictionaryDetailManager.remove(obj.data.hsddId);
                        });
                    }
                },
                {
                    name: 'edit', handler: function (obj) {
                        layui.form.val('dictionaryDetailForm', obj.data);
                        dictionaryDetailManager.isAdd = false;
                        dictionaryDetailManager.openFormLayer();
                    }
                }
            ]
        };
    }
}
dictionaryDetailManager.init();